package com.gears_software.bbnetwork.activitys;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;
import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.datas.finders.IBBNetworkFinder;
import com.gears_software.bbnetwork.datas.finders.impl.IBBNetworkFinderImpl;
import com.gears_software.bbnetwork.datas.models.ResultModel;
import com.gears_software.bbnetwork.datas.models.ReturnCode;
import com.gears_software.bbnetwork.utlis.BBNetworkSharedPref_;
import com.gears_software.bbnetwork.utlis.CommonUtils;
import com.gears_software.bbnetwork.utlis.ToastUI;
import com.gears_software.common.net.NetworkUtil;

@SuppressLint("ResourceAsColor")
@EActivity(R.layout.activity_baodan)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class BaoDanActivity extends Activity {
	
	@ViewById
	EditText edit_content;//内容
	
	@Bean(IBBNetworkFinderImpl.class)
	IBBNetworkFinder finder;
	
	@Pref
	BBNetworkSharedPref_ sp;
	
	@Bean
	ToastUI toast;
	
	@ViewById
	TextView text_submit;//提交按钮
	
	String userid; //用户id
	String content;//内容
	
	@AfterViews
	void onCreate(){
		edit_content.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				String str = edit_content.getText().toString();
				if(str!=null&&!str.equals("")){
					text_submit.setTextColor(getResources().getColor(R.color.col_white));
				}else{
					text_submit.setTextColor(getResources().getColor(R.color.bd_text));
				}
			}
		});
	}
	
	@Click
	void text_back(){
		finish();
	}
	
	//提交
	@Click
	void text_submit(){
		boolean isnet = NetworkUtil.checkNetworkInfo(this);
		if(!isnet){
			showTxt("请检查网络是否正常！");
			return;
		}
		userid = sp.userId().get();
		content = edit_content.getText().toString();
		if(userid==null||userid.equals("")){
			showTxt("用户没有登录");
			return;
		}
		
		if(content==null||content.equals("")){
			showTxt("请输入报单信息");
			return;
		}
		CommonUtils.showDialog(this);
		submitBaodan();
	}
	
	
	@Background
	void submitBaodan(){
		ResultModel<String> res = finder.getApply(userid, content);
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			Intent i = new Intent(this,BaoDanSucceedActivity_.class);
			startActivity(i);
			finish();
		}else{
			Intent i = new Intent(this,BaoDanFailureActivity_.class);
			startActivity(i);
		}
		closeDialog();
	}
	
	@UiThread
	void closeDialog(){
		CommonUtils.closeDialog();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		 AVAnalytics.onResume(this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AVAnalytics.onPause(this);
	}

	@UiThread
	void showTxt(String txt){
		toast.ShowToast(txt);
	}
}
